/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import viewcontrollers.util.XMLParser;

/**
 *
 * @author Ahmed Kotb
 * 
 */

public class SendMail {

    public void send(String to, String subject, String body) {
        SendMail_New(to, subject, body);
    }

    public void SendMail_New(String to, String subject, String body) {
        try {
            String is=this.getClass().getClassLoader().getResource("configfile.xml").getPath();
            XMLParser xmlparser = new XMLParser(is);
            
            final String username = xmlparser.getSupportMail();
            final String password = xmlparser.getSupportPassword();
            final String server=xmlparser.getServer();
            final String port=xmlparser.getServerPort();
            
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            Properties props = System.getProperties();
            props.setProperty("mail.smtp.host", server);
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", port);
            props.setProperty("mail.smtp.socketFactory.port",port);
            props.put("mail.smtp.auth", "true");
            
            Session session = Session.getDefaultInstance(props, new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(xmlparser.getSupportMail()));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, true));
            msg.setSubject(subject);
            msg.setText(body);
            msg.setSentDate(new Date());
            Transport.send(msg);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * Send Mail Contains Attachments
     * @param subject
     * @param from
     * @param fileName 
     */
    public void sendMailWithAttachFile(String subject,final String from ,String fileName ,final String pass) {
        try {
            String is=this.getClass().getClassLoader().getResource("configfile.xml").getPath();
            XMLParser xmlparser = new XMLParser(is);
            final String to = xmlparser.getSupportMail();
            String serverName=xmlparser.getServer();
            String serverPort=xmlparser.getServerPort();
            
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            Properties props = System.getProperties();
            props.setProperty("mail.smtp.host", serverName);
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", serverPort);
            props.setProperty("mail.smtp.socketFactory.port", serverPort);
            props.put("mail.smtp.auth", "true");
            
            Session session = Session.getDefaultInstance(props, new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, pass);
                }
            });
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, true));
            message.setSubject(subject);
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("Here's the file");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(fileName);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(subject);
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            message.setSentDate(new Date());
            Transport.send(message);
            System.out.println("Mail Sent Successfully...");
        } catch (MessagingException e) {
            System.out.println("Mail Sent Failure...");
            e.printStackTrace();
        }
    }
}
